﻿using System;
using System.IO;

namespace CSharpHelpers.IOs
{
    class FileMaskHelper
    {
        public static readonly string AppTempPath = Path.GetTempPath();
        public static readonly string IniPathBak = AppTempPath + "BAK\\";
        public static readonly string IniBak = ".bak";

        /// <summary>
        /// 写加密配置文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="dirName">路径</param>
        /// <param name="isCheckExist">是否判断文件存在</param>
        /// <returns></returns>
        public static bool WriteIniFileMask(string fileName, string dirName, bool isCheckExist = false)
        {
            //文件夹信息
            var dir = new DirectoryInfo(dirName);

            //如果非根路径且是系统文件夹则跳过
            if (null != dir.Parent && dir.Attributes.ToString().ToLower().IndexOf("system", StringComparison.Ordinal) > -1)
            {
                return false;
            }


            bool isExistConfigFile = false;
            FileInfo[] finfo = dir.GetFiles();
            string fname = string.Empty;
            for (int i = 0; i < finfo.Length; i++)
            {
                fname = finfo[i].Name;

                if (fileName.EndsWith(fname))
                {
                    byte[] buffer = FileUtil.EncryptionReadFile(finfo[i].FullName);
                    if (buffer != null)
                    {
                        isExistConfigFile = true;
                        FileUtil.WriteINI(buffer, finfo[i].FullName);
                    }
                }
            }

            if (isCheckExist)
            {
                return isExistConfigFile;
            }

            return false;
        }

        public static void WriteBakIniFile(string bakFileName, string fileName, string filePath)
        {
            //文件夹信息
            var dir = new DirectoryInfo(IniPathBak);

            //如果非根路径且是系统文件夹则跳过
            if (null != dir.Parent && dir.Attributes.ToString().ToLower().IndexOf("system", StringComparison.Ordinal) > -1)
            {
                return;
            }
            //取得所有文件
            FileInfo[] finfo = dir.GetFiles();
            string fname = string.Empty;
            for (int i = 0; i < finfo.Length; i++)
            {
                fname = finfo[i].Name;

                //判断文件是否包含查询名
                if (fname.ToLower().IndexOf(bakFileName, StringComparison.Ordinal) > -1)
                {
                    byte[] buffer = FileUtil.LocalBAKXML(finfo[i].FullName);
                    if (buffer != null)
                    {
                        FileUtil.WriteINI(buffer, filePath + fileName);
                    }
                }
            }

        }

        /// <summary>
        /// 写临时文件
        /// </summary>
        /// <param name="bakFileName"></param>
        /// <param name="fileName"></param>
        /// <param name="dirName"></param>
        public static void DecryptAndSaveTempFile(string bakFileName, string fileName, string dirName)
        {
            //文件夹信息
            var dir = new DirectoryInfo(dirName);

            //如果非根路径且是系统文件夹则跳过
            if (null != dir.Parent && dir.Attributes.ToString().ToLower().IndexOf("system", StringComparison.Ordinal) > -1)
            {
                return;
            }

            //取得所有文件
            FileInfo[] finfo = dir.GetFiles();
            string fname = string.Empty;
            for (int i = 0; i < finfo.Length; i++)
            {
                fname = finfo[i].Name;

                //判断文件是否包含查询名
                if (fileName.EndsWith(fname))
                {
                    byte[] buffer = FileUtil.DecryptReadFile(finfo[i].FullName);
                    if (buffer != null)
                    {
                        FileUtil.WriteBakFile(buffer, IniPathBak + fileName + IniBak, bakFileName);
                    }
                }

            }
        }
    }
}
